package repository

import (
	"context"
	"server_go/internal/model"
)

type AdminRepository interface {
	GetAdminByAccount(ctx context.Context, account string) (*model.Admin, error)
	DeleteUser(ctx context.Context, userID uint) error
	GetUserByID(ctx context.Context, userID uint) (*model.Users, error)
	GetUsers(ctx context.Context) ([]*model.Users, error)
}

func NewAdminRepository(
	repository *Repository,
) AdminRepository {
	return &adminRepository{
		Repository: repository,
	}
}

type adminRepository struct {
	*Repository
}

func (r *adminRepository) GetAdminByAccount(ctx context.Context, account string) (*model.Admin, error) {
	var admin model.Admin
	err := r.DB(ctx).Where("account = ? ", account).First(&admin).Error
	if err != nil {
		return nil, err
	}
	return &admin, nil
}
func (r *adminRepository) GetUserByID(ctx context.Context, userID uint) (*model.Users, error) {
	var user model.Users
	err := r.DB(ctx).Where("id = ?", userID).First(&user).Error
	if err != nil {
		return nil, err
	}
	return &user, nil
}
func (r *adminRepository) DeleteUser(ctx context.Context, userID uint) error {
	return r.DB(ctx).Where("id = ?", userID).Delete(&model.Users{}).Error
}

func (r *adminRepository) GetUsers(ctx context.Context) ([]*model.Users, error) {
	var users []*model.Users
	err := r.DB(ctx).Where("type = ?", 2).Find(&users).Error
	if err != nil {
		return nil, err
	}
	return users, nil
}
